package com.example.spsaveqqty;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.spsaveqqty.userManage.User;
import com.example.spsaveqqty.userManage.UserDao;

public class RegActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et_number;
    private EditText et_password;
    private EditText et_password2;
    private Button btn_back;
    private Button btn_submit;

    private UserDao userdao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        //初始化页面
        init();
        //创建userdao对象
        userdao = new UserDao(this);
    }
    private void init() {
        et_number = (EditText) findViewById(R.id.et_reg_number);
        et_password = (EditText) findViewById(R.id.et_reg_password);
        et_password2 = (EditText) findViewById(R.id.et_reg_password2);
        btn_back = (Button) findViewById(R.id.btn_back);
        btn_submit = (Button) findViewById(R.id.btn_submit);
        //设置按钮的点击事件
        btn_back.setOnClickListener(this);
        btn_submit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String username;
        String password;
        String password2;
        User user = new User();
        switch (v.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_submit:
                username = et_number.getText().toString().trim();
                password = et_password.getText().toString().trim();
                password2 = et_password2.getText().toString().trim();
                if(TextUtils.isEmpty(username)||TextUtils.isEmpty(password)||TextUtils.isEmpty(password2)){
                    Toast.makeText(this, "请输入完整的注册信息", Toast.LENGTH_SHORT).show();
                }else if (!password.equals(password2)){
                    Toast.makeText(this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
                }else if(username.length()>16||password.length()>16||password2.length()>16){
                    Toast.makeText(this, "账号和密码不能超过16位！", Toast.LENGTH_SHORT).show();
                }else{
                    int count = userdao.getUserCount(username);
                    if(count==0){//代表此账号无人注册过
                        user.setUsername(username);
                        user.setPassword(password);
                        userdao.insert(user);
                        Toast.makeText(this,"注册成功",Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(RegActivity.this, MainActivity.class);
                        intent.putExtra("flag", "clear");
                        startActivity(intent);
                        finish();
                    }else{
                        Toast.makeText(this,"该账号已被注册",Toast.LENGTH_SHORT).show();
                        finish();
                    }
                }
                break;
        }
    }
}
